/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.hs.srm.common.utils;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * Redis工具类
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-07-17 21:12
 */
@Component
public class RedisUtils {
	/**  默认过期时长，单位：秒 */
	public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
	/**  不设置过期时长 */
	public final static long NOT_EXPIRE = -1;
	@Autowired
	private RedisTemplate<String, ?> redisTemplate;
	@Resource(name = "redisTemplate")
	private ValueOperations<String, String> valueOperations;
	@Resource(name = "redisTemplate")
	private HashOperations<String, String, Object> hashOperations;
	@Resource(name = "redisTemplate")
	private ListOperations<String, Object> listOperations;
	@Resource(name = "redisTemplate")
	private SetOperations<String, Object> setOperations;
	@Resource(name = "redisTemplate")
	private ZSetOperations<String, Object> zSetOperations;

	public void delete(String key) {
		this.redisTemplate.delete(key);
	}

	public Set<String> keys(String keyPattern) {
		return this.redisTemplate.keys(keyPattern);
	}

	public void dels(Set<String> keys) {
		Iterator<String> it = keys.iterator();
		while(it.hasNext()) {
			String keyStr = it.next();
			this.redisTemplate.delete(keyStr);
		}
	}

	public String get(String key) {
		return this.get(key, RedisUtils.NOT_EXPIRE);
	}

	public <T> T get(String key, Class<T> clazz) {
		return this.get(key, clazz, RedisUtils.NOT_EXPIRE);
	}

	public <T> T get(String key, Class<T> clazz, long expire) {
		String value = this.valueOperations.get(key);
		if(expire != RedisUtils.NOT_EXPIRE) {
			this.redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value == null ? null : this.fromJson(value, clazz);
	}

	public String get(String key, long expire) {
		String value = this.valueOperations.get(key);
		if(expire != RedisUtils.NOT_EXPIRE) {
			this.redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value;
	}

	public void set(String key, Object value) {
		this.set(key, value, RedisUtils.DEFAULT_EXPIRE);
	}

	public void set(String key, Object value, long expire) {
		this.valueOperations.set(key, this.toJson(value));
		if(expire != RedisUtils.NOT_EXPIRE) {
			this.redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
	}

	/**
	 * Object转成JSON数据
	 */
	private String toJson(Object object) {
		if(object instanceof Number || object instanceof Boolean || object instanceof CharSequence) {
			return object.toString();
		}
		return JSON.toJSONString(object);
	}

	/**
	 * JSON数据，转成Object
	 */
	private <T> T fromJson(String json, Class<T> clazz) {
		return JSON.parseObject(json, clazz);
	}

}
